package com.sendpo.zjx.sendpo.activity;

import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.sendpo.zjx.sendpo.R;
import com.sendpo.zjx.sendpo.net.HttpCode;
import com.sendpo.zjx.sendpo.net.HttpUrl;
import com.sendpo.zjx.sendpo.table.User;
import com.sendpo.zjx.sendpo.utils.Tools;
import com.sendpo.zjx.sendpo.utils.Xutils;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 朱俊鑫 on 2018
 */

public class FeedbackActivity extends AppCompatActivity {


    @BindView(R.id.backbt)
    RelativeLayout backbt;
    @BindView(R.id.head)
    RelativeLayout head;
    @BindView(R.id.edit_opinion)
    EditText editOpinion;
    @BindView(R.id.submitbt)
    Button submitbt;
    private Dialog loadingdialog
            ;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedback);
        ButterKnife.bind(this);
        Tools.initStatus(this, R.color.black);
        initComponent();

    }

    private void initComponent() {
        this.loadingdialog = new Dialog(this);
        this.loadingdialog.setContentView(R.layout.dialog_loading);
        this.loadingdialog.setCanceledOnTouchOutside(false);
        this.loadingdialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    }

    @OnClick(value = {R.id.backbt,R.id.submitbt})
    public void onMyClick(View view) {
        switch (view.getId()) {
            case R.id.backbt:
                finish();
                break;
            case R.id.submitbt:
                if (TextUtils.isEmpty(this.editOpinion.getText().toString().trim()))
                {
                    Toast.makeText(getApplicationContext(), "内容不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                addOption(editOpinion.getText().toString().trim());
                break;
        }
    }
     public void addOption(String content){
        List<User> userList= Xutils.queryAll(getApplication(),User.class);
          if(userList==null||userList.size()==0){
              return;
          }
     loadingdialog.show();
         Map<String,String> map=new HashMap<>();
      map.put("userId",userList.get(0).getUserId()+"");
         map.put("userOpinion",content);
         Xutils.POST(HttpUrl.optionadd, map, new Callback.CommonCallback<String>() {
             @Override
             public void onSuccess(String result) {
                 loadingdialog.dismiss();
                 editOpinion.getText().clear();
                 try {
                     JSONObject jsonObject=new JSONObject(result);
                     Toast.makeText(FeedbackActivity.this.getApplicationContext(), "" +jsonObject.getString("message"), Toast.LENGTH_SHORT).show();
                switch (jsonObject.getInt("state")){
                    case HttpCode.SUCCESS:

                        break;
                }
                 } catch (JSONException e) {
                     e.printStackTrace();
                 }
             }

             @Override
             public void onError(Throwable ex, boolean isOnCallback) {
                 editOpinion.getText().clear();
                 loadingdialog.dismiss();
             }

             @Override
             public void onCancelled(CancelledException cex) {

             }

             @Override
             public void onFinished() {

             }
         });
     }


}
